import React, { Component } from 'react';
import SimpleTemperature from './temperatureSimple/temperature';


import Temperature from './temperature/temperature';
import { getTemplatureData } from './templatureNet';


class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      currentPage: 1,
      isloading: false,
      isSimple: true,
      totalPage: 1,
      inputPage: 1,
      templatrueData: null,
      headerHiddenStr: "flex",
    };

  }


  componentDidMount() {


    this.loadTemplatureData(this.state.currentPage);
  }

  loadTemplatureData = (reqPage) => {
    if (!this.props.match.params.patientId) {
      return
    }

    this.setState({
      isloading: true,
    })
    let params = {
      patientNo: this.props.match.params.patientId,
      page: reqPage,
    }

    getTemplatureData(params, (resp) => {
      let { code, data = {}, msg } = resp;


      if (code == 0) {

        let isSimple = data["stauts"] == "正常" ? true : false;


        this.setState({
          isSimple: isSimple,
          templatrueData: data,
          totalPage: data["totalPage"],
          currentPage: reqPage
        })

      } else {

        // console.log('hulinhua-------111', resp);
        this.setState({
          templatrueData: null,
        })
      }

      this.setState({
        isloading: false,

      })
    })

  }


  _renderTemplature = () => {
    if (this.state.isSimple) {
      return (
        <SimpleTemperature sourceData={this.state.templatrueData} />
      )

    } else {
      return (
        <Temperature sourceData={this.state.templatrueData} />
      )

    }

  }

  render() {
    return (
      <div id='oldDoc' className="App">

        {this.state.isloading ? <div className="a_load"></div> : null}

        <div style={{ display: this.state.headerHiddenStr, flexDirection: 'row', justifyContent: 'center', justifyItems: 'center', paddingTop: '10px' }}>
          <button style={{ marginRight: 10 }}
            onClick={() => {
              this.setState({ headerHiddenStr: "none" }, () => {
                window.print();
                setTimeout(() => {
                  this.setState({ headerHiddenStr: "flex" })
                }, 300)
              })


              // var oldDoc = window.document.getElementById("oldDoc").innerHTML;



              // var oWin = window.open("", "_blank");
              // oWin.document.body.innerHTML = window.document.getElementById("printTemplature").innerHTML;

            }}>
            打印
        </button>
          <button onClick={() => {
            if (this.state.isloading) {
              return
            }

            let nextPage = this.state.currentPage - 1;
            if (nextPage < 1) {
              nextPage = 1
            }
            this.loadTemplatureData(nextPage);


          }} style={{ paddingLeft: 10, paddingRight: 10 }}>上一页</button>
          <div style={{ paddingLeft: 10, paddingRight: 10 }}>第{this.state.currentPage} /{this.state.totalPage}页</div>

          <button onClick={() => {
            if (this.state.isloading) {
              return
            }

            let nextPage = this.state.currentPage + 1;
            if (nextPage > this.state.totalPage) {
              nextPage = this.state.totalPage
            }
            this.loadTemplatureData(nextPage);

          }} style={{ paddingLeft: 10, paddingRight: 10 }}>下一页</button>

          <input onChange={(event) => {
            let pageNum = event.target.value;
            this.setState({
              inputPage: pageNum
            })
          }} style={{ marginLeft: 10, marginRight: 5, width: 40 }} type="number" value={this.state.inputPage} min="1" max={this.state.totalPage} />
          <button onClick={() => {
            let willPage = this.state.inputPage;
            if (willPage < 1) {
              willPage = 1;
            }
            if (willPage > this.state.totalPage) {
              willPage = this.state.totalPage;
            }
            this.loadTemplatureData(willPage);

          }}>跳转该页</button>
        </div>


        <div id='printTemplature'>
          {this.state.templatrueData ? this._renderTemplature() : null}
        </div>
        <div style={{ width: "100%", textAlign: "center", paddingTop: 5, fontSize: 12 }}>第{this.state.currentPage} /{this.state.totalPage}页</div>


      </div>
    );
  }
}

export default App;






